// pages/cleaner/cleaner.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{
      product: {
        id: '',
        price: '',
        name: '',
        main_img: '',
        time: '',
        unit: '',
        service_category_id: '',
        content: ''
      },
      address: {
        id: '',
        name: '',
        phone: '',
        address: '',
        province_id: '',
        city_id: '',
        district_id: ''
      },
      time: {
        start_at: '',
        end_at: '',
        hours: 1
      },
      cleaner: {
        id: '',
        wx_avatar_url: '',
        real_name: ''
      },
      coupon: {
        id: '',
        price: '',
      },
      remark: '',
      totalPrice: 0,
      textarea: true
    },
    provider: {},
    commentList: {},
    isCollection: 0,
    maskShow: false,
    scType: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onload')
    this.setData({
      info: JSON.parse(options.info)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onshow')
    this.getProvider()
  },

/**
 * 获取当前ID的保洁详情
 */
  getProvider: function() {
    let that = this
    wx.showLoading({
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/provider/info?access_token=' + accessToken,
        data: {
          provider_id: that.data.info.cleaner.id
        },
        success: res => {
          if(res.data.status === 1) {
            that.setData({
              'info.cleaner': res.data.data.info,
              commentList: res.data.data.comment_list,
              isCollection: res.data.data.is_collectioned
            })
            wx.hideLoading()
          }
        }
      })
    })
  },

  /**
   * 收藏
   */
  addFavorite: function() {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/customer/collection?access_token=' + accessToken,
        data: {
          provider_id: that.data.info.cleaner.id
        },
        success: res => {
          if(res.data.status === 1) {
            wx.showToast({
              icon: 'none',
              title: '收藏成功',
              success: function() {
                that.getProvider()
              }
            })
          }
        }
      })
    })
  },

  onMask: function() {
    this.setData({
      maskShow: false
    })
  },

  showServ: function() {
    let that = this
    wx.showLoading({
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/provider/get-product-by-provider-id?access_token=' + accessToken,
        data: {
          provider_id: that.data.info.cleaner.id
        },
        success: res => {
          that.setData({
            scType: res.data.data,
            maskShow: true
          })
          wx.hideLoading()
        }
      })
    })
    
  },

  creatOrder: function(e) {
    this.setData({
        'info.product.id': e.currentTarget.id
    })
    wx.navigateTo({
      url: '../../product/product?info=' + JSON.stringify(this.data.info),
    })
  },

  onShareAppMessage: function(res) {
    return {
      title: '随心召唤洁净无限',
      path: 'pages/cleaner/detail/detail?info=' + JSON.stringify(this.data.info)
    }
  }
})